package com.ruoyi.web.zbAdmin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.zbAdmin.controller.param.BaseUserQuery;
import com.ruoyi.web.zbAdmin.domain.CaiFeedback;
import com.ruoyi.web.zbAdmin.mapper.CaiZhaobiaoPropertyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 建议反馈
 * @author liandyao
 * @date 2022/12/15 10:47
 */
@Api(tags="3001 反馈意见，注销账号")
@RestController
@RequestMapping("/zbApi/feedback")
@Slf4j
public class CaiFeedbackController extends BaseController {

    @Autowired
    private CaiZhaobiaoPropertyMapper userMapper;

    public static final String collectionName = "cai_feedback";

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取用户列表
     */
    @ApiOperation("反馈列表")
    @GetMapping("/list")
    public TableDataInfo list(BaseUserQuery userQuery) {

        log.info("=====>"+JSONUtil.toJsonStr(userQuery));
        MongoTemplate mongoTemplate = this.userMapper.getMongoTemplate();

        Query query = new Query();
        if(StrUtil.isNotBlank(userQuery.getPhone())){
            query.addCriteria(Criteria.where("userPhone").is(userQuery.getPhone()));
        }
 

        // 设置起始页和每页查询条数
        Pageable pageable = PageRequest.of(userQuery.getCurPage(), userQuery.getPageSize(), Sort.by(Sort.Order.desc("createTime")));
        // 查询记录总数
        long totalCount = mongoTemplate.count(query,collectionName);
        //查询分页后的记录
        List<CaiFeedback> result = mongoTemplate.find(query.with(pageable), CaiFeedback.class, collectionName);

        return TableDataInfo.success(result,totalCount);
    }


    @ApiOperation("获取反馈信息")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable String id) {
        MongoTemplate mongoTemplate = this.userMapper.getMongoTemplate();
        CaiFeedback user = mongoTemplate.findById(id,CaiFeedback.class,collectionName);
        if(user==null){
            return AjaxResult.error("不存在!");
        }
        return AjaxResult.success("成功",user);
    }





}
